package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.adapter.TagAdapter;
import flc.ast.databinding.FragmentTopicBinding;
import stark.common.api.StkApi;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.bean.StkChildResourceBean;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TopicFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TopicFragment extends BaseNoModelFragment<FragmentTopicBinding> {

    private TagAdapter tagAdapter;
    private List<Fragment> fragments = new ArrayList<>();
    private String[] tabTitle = new String[]{};


    @Override
    protected int onCreate() {
        return R.layout.fragment_topic;
    }

    @Override
    protected void initView() {
        mDataBinding.rvTagList.setLayoutManager(new GridLayoutManager(mContext, 2));
        tagAdapter = new TagAdapter();
        mDataBinding.rvTagList.setAdapter(tagAdapter);
        tagAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        tabTitle = new String[]{getString(R.string.hot), getString(R.string.game_name1), getString(R.string.game_name2), getString(R.string.game_name3), getString(R.string.game_name4), getString(R.string.game_name5)};
        getTagData();
        setTabData();
    }

    private void getTagData() {
        StkResApi.getTagResourceList(null, AppConfig.TAG_DATA, StkApi.createParamMap(0, 4), false,
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (result != null && result.size() > 0) {
                                tagAdapter.setList(result);
                            }
                        }
                    }
                });
    }

    private void setTabData() {
        List<String> list = new ArrayList<>();
        list.add(AppConfig.TOPIC_DATA1);
        list.add(AppConfig.TOPIC_DATA2);
        list.add(AppConfig.TOPIC_DATA3);
        list.add(AppConfig.TOPIC_DATA4);
        list.add(AppConfig.TOPIC_DATA5);
        fragments.add(new HotFragment().newInstance());
        mDataBinding.tlVideo.addTab(mDataBinding.tlVideo.newTab().setText(tabTitle[0]));
        for (int i = 0; i < list.size(); i++) {
            fragments.add(new TabFragment().newInstance(list.get(i)));
            mDataBinding.tlVideo.addTab(mDataBinding.tlVideo.newTab().setText(tabTitle[i + 1]));
        }
        mDataBinding.vpTopic.setAdapter(new ArticleAdapter(getActivity().getSupportFragmentManager()));
        mDataBinding.tlVideo.setupWithViewPager(mDataBinding.vpTopic);
        //给tab每个item设置值
        for (int i = 0; i < mDataBinding.tlVideo.getTabCount(); i++) {
            TabLayout.Tab tab = mDataBinding.tlVideo.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i));
            }
        }
        // 设置默认第一个被选中
        View view = mDataBinding.tlVideo.getTabAt(0).getCustomView();
        TextView tv = view.findViewById(R.id.tvTab);
        ImageView iv = view.findViewById(R.id.ivTab);
        iv.setVisibility(View.VISIBLE);
        tv.setTextColor(Color.parseColor("#000000"));
        tv.setTextSize(18);
        //设置TabLayout点击事件
        mDataBinding.tlVideo.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                TextView tv = view.findViewById(R.id.tvTab);
                ImageView iv = view.findViewById(R.id.ivTab);
                tv.setTextColor(Color.parseColor("#000000"));
                tv.setTextSize(18);
                if (tab.getPosition() == 0) {
                    iv.setVisibility(View.VISIBLE);
                } else {
                    iv.setVisibility(View.GONE);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                TextView tv = view.findViewById(R.id.tvTab);
                tv.setTextColor(Color.parseColor("#B9B9B9"));
                tv.setTextSize(14);
                iv.setVisibility(View.GONE);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private class ArticleAdapter extends FragmentStatePagerAdapter {
        public ArticleAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        //此方法用来显示tab上的名字
        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitle[position];
        }
    }

    //自定义Tab布局
    private View getTabView(int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_tab, null);
        TextView text = view.findViewById(R.id.tvTab);
        text.setText(tabTitle[i]);
        return view;
    }

}